<?php

namespace App\Listeners;

use App\Constant\NewFlowEnum;
use App\Models\LoanApply;
use App\Models\NewFlowOrder;
use App\Models\User;
use App\Models\UserCertOcr;
use App\Models\UserLoanRequest;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\Log;

class NewFlowOrderEventListener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Description: 记录进件流程中进入节点的时间点 NewFlowEnum
     * Author: cg
     * DateTime: 2019-03-21 11:10
     * @param  object  $event
     * @return void
     */
    public function handle($event)
    {
        try {
            $enum = $event->enum;
            $loan = $event->loan;
            $loan_id = $event->loan_id;
            $oth_data = $event->data;
            Log::debug('NewFlowOrderEventListener-data:'. json_encode(['enum'=>$enum, 'loan'=>$loan, 'loan_id', $loan_id, 'oth_data'=>$oth_data], JSON_UNESCAPED_UNICODE));
            switch ($enum) {
                case NewFlowEnum::NewFlowLoanInit:
                    Log::debug("NewFlowOrderEvent-申请-" . json_encode($loan, 256));
                    // 用户刚开始申请
                    $cert = UserCertOcr::where('user_id', $loan->user_id)->first();
                    $name = $cert->name;
                    $num = $cert->num;
                    $users = User::find($loan->user_id);
                    $phone = $users->phone_num;
                    $data = [
                        'loan_requests_id' => $loan->id,
                        'user_id' => $loan->user_id,
                        'name' => $name,
                        'num' => $num,
                        'period' => $loan->period,
                        'phone' => $phone,
                        'source' => $users->source ?? '' ,
                        'product_name' => '快闪卡贷',
                        'user_apply_limit' => $loan->loan_money,
                        'user_apply_time' => $loan->apply_time
                    ];
                    NewFlowOrder::create($data);
                    break;
                case NewFlowEnum::NewFlowRiskInit:
                    // 提交风控
                    Log::debug("NewFlowOrderEvent-提交风控-loanId-" . $loan_id);
                    $newFlow = NewFlowOrder::where('loan_requests_id', $loan_id)->first();
                    Log::debug('NewFlowOrderEventListener-NewFlowRiskInit-:'. json_encode($newFlow, JSON_UNESCAPED_UNICODE));
                    $newFlow->risk_apply_time = date("Y-m-d H:i:s", time());
                    $newFlow->save();
                    break;
                case NewFlowEnum::NewFlowRiskResult:
                    // 风控结果
                    Log::debug("NewFlowOrderEvent-风控结果-loanId-" . json_encode($oth_data, 256));
                    $newFlow = NewFlowOrder::where('loan_requests_id', $loan_id)->first();
                    $newFlow->risk_result_time = date("Y-m-d H:i:s", time());
                    $newFlow->risk_result_limit = $oth_data['limit'];
                    $newFlow->risk_status = $oth_data['status'];
                    $newFlow->message = '风控结果-'.$oth_data['reason'];
                    $newFlow->save();
                    break;
                case NewFlowEnum::NewFlowLoanAuthorize:
                    // 授信申请
                    Log::debug("NewFlowOrderEvent-授信申请-loanId-" . json_encode($loan, 256));
                    $newFlow = NewFlowOrder::where('loan_requests_id', $loan->id)->first();
                    $newFlow->loan_request_apply_time = date("Y-m-d H:i:s", time());
                    $newFlow->save();
                    break;
                case NewFlowEnum::NewFlowLoanAuthorizeResult:
                    Log::debug("NewFlowOrderEvent-授信结果-" . json_encode($oth_data, 256));
                    // 授信结果
                    $newFlow = NewFlowOrder::where('loan_requests_id', $loan->id)->first();
                    $newFlow->loan_request_result_time = date("Y-m-d H:i:s", time());
                    if($oth_data['code'] == '01'){
                        if(array_key_exists('data', $oth_data)){
                            $authResult = $oth_data['data']['status'] == "S" ? NewFlowOrder::MONEY_STATUS_SUCCESS : NewFlowOrder::MONEY_STATUS_FAILURE;
                            $limit = array_key_exists('amount', $oth_data['data']) ? $oth_data['data']['amount'] : 0;
                            $capital_name = array_key_exists('channel', $oth_data['data']) ? $oth_data['data']['channel'] : '';
                        }else{
                            $authResult = NewFlowOrder::MONEY_STATUS_FAILURE;
                            $limit = 0;
                            $capital_name = '';
                        }
                    }else{
                        $authResult = NewFlowOrder::MONEY_STATUS_FAILURE;
                        $limit = 0;
                        $capital_name = '';
                    }
                    $newFlow->loan_request_status = $authResult;
                    $newFlow->loan_request_limit = $limit;
                    $newFlow->capital_name = $capital_name;
                    $newFlow->message = $oth_data['message'] ? '授信结果-'.$oth_data['message'] : '';
                    $newFlow->save();
                    break;
                case NewFlowEnum::NewFlowGetMoney:
                    // 确认要款 申请放款接口
                    Log::debug("NewFlowOrderEvent-确认要款-loanId-" . $loan_id);
                    $newFlow = NewFlowOrder::where('loan_requests_id', $loan_id)->first();
                    $newFlow->loan_commit_time = date("Y-m-d H:i:s", time());
                    $newFlow->save();
                    break;
                case NewFlowEnum::NewFlowLoanApply:
                    // 放款
                    Log::debug("NewFlowOrderEvent-放款结果-" . json_encode($loan, 256) . 'oth:' . json_encode($oth_data, 256));
                    $newFlow = NewFlowOrder::where('loan_requests_id', $loan->loan_request_id)->first();
                    $newFlow->loan_status = LoanApply::getLoanStatusByRes($oth_data) == 1 ? NewFlowOrder::DOWN_MONEY_STATUS_SUCCESS : NewFlowOrder::DOWN_MONEY_STATUS_FAILURE;
                    $newFlow->loan_time = array_key_exists('data', $oth_data) ? $oth_data['data']['loanTime'] : null;
                    $getDetail = LoanApply::getDetailByTransMoneyList(array_key_exists('data', $oth_data) ? $oth_data['data']['transMoneyList'] : '');
                    if ($getDetail['arrival_account']) {
                        $newFlow->loan_limit = $getDetail['arrival_account'];
                    }
                    $newFlow->message = $oth_data['code'] == '01' ? '放款结果-'.$oth_data['message'] : '放款结果-'.$oth_data['message'];
                    $newFlow->save();
                    break;
            }
        } catch (\Exception $e){
            Log::debug("NewFlowOrderEvent-Exception-" . $e->getMessage() . ';file:'.$e->getFile() .';line:' . $e->getLine());
        }


    }



}
